package net.bcxuexi.server.model;

import java.io.IOException;
import java.net.Socket;

import net.bcxuexi.config.Config;
import net.bcxuexi.server.SocketManager;
import net.bcxuexi.server.stream.ReadStream;
import net.bcxuexi.server.stream.StreamData;
import net.bcxuexi.server.stream.WriteStream;
import net.bcxuexi.server.stream.outwrap.OutputDataWrap;
import net.bcxuexi.tools.MyLog;
import net.bcxuexi.tools.StringUtils;

public class SocketModel {
	/**socket编号*/
	private String connId;
	/**是否验证connId,即客户端发送过来的connId和connId是否相同；proxy类型自动验证通过*/
	private boolean hasValidConnId=false;
	/**域名*/
	private String socketDomain;
	/**
	 * socket类型
	 * control:控制信息socket
	 * data:数据传输
	 * proxy:外部连接代理端口服务的socket
	 */
	private String type;
	
	/**
	 * 如果type类型为data或者control时使用。
	 * 当前socket所属的控制连接clientkey，拥有同样的clientkey说明是同一个客户端.
	 * 
	 * 如果type是proxy类型，那么clientkey是它需要访问的客户端
	 */
	private String clientkey;
	private ServerSocketModel serverSocketModel;
	private Socket socket;
	
	/**socket信息读取对象*/
	private ReadStream readStream;
	/**socket信息写对象*/
	private WriteStream writeStream;
	public String getConnId() {
		return connId;
	}
	public void setConnId(String connId) {
		this.connId = connId;
	}
	
	public String getSocketDomain() {
		return socketDomain;
	}
	public void setSocketDomain(String socketDomain) {
		this.socketDomain = socketDomain;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public ServerSocketModel getServerSocketModel() {
		return serverSocketModel;
	}
	public void setServerSocketModel(ServerSocketModel serverSocketModel) {
		this.serverSocketModel = serverSocketModel;
	}
	
	public Socket getSocket() {
		return socket;
	}
	public void setSocket(Socket socket) {
		this.socket = socket;
	}
	
	public ReadStream getReadStream() {
		return readStream;
	}
	public void setReadStream(ReadStream readStream) {
		this.readStream = readStream;
	}
	public WriteStream getWriteStream() {
		return writeStream;
	}
	public void setWriteStream(WriteStream writeStream) {
		this.writeStream = writeStream;
	}
	/**
	 * 是否验证connId,即客户端发送过来的connId和connId是否相同；
	 * proxy类型自动验证通过
	 * 
	 */
	public boolean getHasValidConnId() {
		return hasValidConnId;
	}
	public void setHasValidConnId(Boolean hasValidConnId) {
		this.hasValidConnId = hasValidConnId;
	}
	
	
	
	public String getClientkey() {
		return clientkey;
	}
	public void setClientkey(String clientkey) {
		this.clientkey = clientkey;
	}
	@Override
	public String toString() {
		String msg = "socket类型："+type+",connId="+connId;
		
		return msg;
	}
	public boolean isActive(){
		if(socket==null||socket.isClosed()){
			return false;
		}
		return true;
	}
	public void closeSocket(String msg){
		MyLog.info(msg);
		try {
			if(socket!=null&&!socket.isClosed()){
				socket.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(readStream!=null){
				readStream.stopRead();
			}
			if(writeStream!=null){
				writeStream.stopWrite();
			}
			if(socket!=null&&!StringUtils.isBlack(clientkey)){//保证只调用一次
				if(Config.SocketType.proxy.equals(type)){
					SocketModel controSocketModel = SocketManager.getInstance().findControlSocketModelByClientKey(clientkey);
					OutputDataWrap out = new OutputDataWrap();
					StreamData streamData = out.scProxyClosed(connId);
					streamData.setSocketModel(controSocketModel);
					try {
						controSocketModel.getWriteStream().addData(streamData);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			readStream=null;
			writeStream=null;
			socket=null;
		}
	}
}
